package com.dragon.mmochat.model.task;

import com.dragon.mmochat.model.MmochatSerial;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.util.MmochatUtil;

//临时任务
public class MmochatTempTask extends MmochatTask {
	private static final long serialVersionUID = -3880720424254294116L;
	private static MmochatSerial serial = new MmochatSerial();
	private static int lastSavedId = -1;

	public MmochatTempTask() {
		// TODO:空构造只限Gson使用
	}

	public MmochatTempTask(boolean init) {
		if (init) {
			int curTaskId = -1 * serial.addAndGetSerialId();
			if (-1 * curTaskId == Integer.MAX_VALUE) {
				serial.setUniqueId(1);
				curTaskId = -1;
				MmochatDao.decreaseTempTaskSerial(lastSavedId - curTaskId);
				lastSavedId = curTaskId;
			}
			setTaskId(curTaskId);
			if (lastSavedId - curTaskId >= 1000) {
				MmochatDao.decreaseTempTaskSerial(lastSavedId - curTaskId);
				lastSavedId = curTaskId;
			}
		}
	}

	public MmochatTempTask getClone() {
		return (MmochatTempTask) MmochatUtil.deepClone(this);
	}

	public static void initSerial(int initValue) {
		serial.setUniqueId(-1 * initValue);
		lastSavedId = initValue;
	}

	public static int getCurTaskId() {
		return -1 * serial.getSerialId();
	}

}
